<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * @abstract 支付订单接口
 * @author zxj
 */
class PayOrder extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
    }

    function save_pay_order()
    {
        $post_data = gp();
        log_output($post_data);
        $userFirst = $this->common_model->selectFirst('user',
            array('account'=>$post_data['user'],'password'=>md5($post_data['key']))
        );
        if(!$userFirst){
            json_output($post_data,'request failed, invalid user',false);
        }
        $payOrders = json_decode($post_data['orders'],true);
        if(!$payOrders || !is_array($payOrders)){
            json_output($payOrders,'Invalid payment information',false);
        }
        $orderFirst = $this->common_model->selectFirst('maintain_product_price',array('oid'=>$payOrders['oid']));
        if(!$orderFirst){
            json_output($orderFirst,'order does not exist',false);
        }
        $setData = array(
            'payment_id'=>$payOrders['payment_id'],
            'paid'=>$payOrders['paid'],
            'agreed'=>$payOrders['agreed'],
            'authorisation_code'=>$payOrders['authorisation_code'],
            'payment_date'=>$payOrders['payment_date'],
            'amount_paid'=>$payOrders['amount_paid']/100
        );
        if($payOrders['agreed']=='Y'){
            $setData['is_accept_price'] = '1';
        }
        $setRs = $this->common_model->update('maintain_product_price',$setData,array('oid'=>$payOrders['oid']));
        json_output($setRs,'succeed',true);
    }


    /**
     * 同意OR拒绝报价
     * AgreeQuotation OR rejectQuotation
     */
    function confirmRepairFlag(){
        $gpData = gp();
        log_output($gpData);
        $userFirst = $this->common_model->selectFirst('user',
            array('account'=>$gpData['user'],'password'=>md5($gpData['key']))
        );
        if(!$userFirst){
            json_output($gpData,'request failed, invalid user',false);
        }
        $products = $gpData['products'];
        if(!$products || !is_array($products)){
            json_output($products,'The data format is not correct',false);
        }
        json_output($gpData,'succeed!',true);

    }




    function get_order_price()
    {
        header('Content-Type: application/json; charset=utf-8');
        //推送数据
        $url = "http://tomrepair.es/~tomrepair/api/getPaymentInfo";
        $data_to_post = array();
        $data_to_post['user'] = 'api@tomrepair.es';
        $data_to_post['token'] = '78de933bd0fbacf02ef2de1116e3338f1c627b07';
        $data_to_post['orderID'] = '56FAE8338A670';
        $data_to_post = json_encode($data_to_post);
        $headers = array();
        $headers[] = 'Authorization: DMCOHh3Y6Zf562MaDU3YzMyRjp3RFP9987trzdLZRZzcjFt';
        $headers[] = 'Content-Type: application/json; charset=utf-8';

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data_to_post);
        $result = curl_exec($curl);
        curl_close($curl);
        //gsh_output(json_decode($result,true));
        echo $result;
    }




}
